package jannex.sql.extensions;

import jannex.Jannex;
import jannex.mapping.ResultSetHandler;
import jannex.internal.ExceptionFactory;
import jannex.sql.TypedPreparedStatement;
import jannex.sql.TypedResultSet;

import java.sql.SQLException;

public class TypedPreparedStatementImpl<E> extends PreparedStatementImpl implements TypedPreparedStatement<E> {

    private final ResultSetHandler<E> handler;

    public TypedPreparedStatementImpl(java.sql.PreparedStatement preparedStatement, Jannex jannex, ResultSetHandler<E> handler) {
        super(preparedStatement, jannex);
        this.handler = handler;
    }

    public TypedResultSet<E> executeQuery() {
        try {
            return new TypedResultSetImpl<>(preparedStatement.executeQuery(), jannex, handler);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    public TypedResultSet<E> executeQuery(String sql) {
        try {
            return new TypedResultSetImpl<>(preparedStatement.executeQuery(sql), jannex, handler);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    public TypedResultSet<E> getResultSet() {
        try {
            return new TypedResultSetImpl<>(preparedStatement.getResultSet(), jannex, handler);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

}
